//////////////////////////////////////////////////////////////////////////////////////////////
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	File			:	"OptionsState.cpp"
//	Authors			:	Jake Castillo,
//	Project			:	Project Aetheryte
//	Date Modified	:	8/7/2013
//	Purpose			:	OptionsState is where the player selects the various different options
//						for the game, including bg volume, sfx volume, resolution, fullscreen. 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//////////////////////////////////////////////////////////////////////////////////////////////

#include "OptionsState.h"


/*static*/ OptionsState* OptionsState::GetInstance( void )
{
	static OptionsState Option_Instance;
	return &Option_Instance;
}

OptionsState::OptionsState(void)
{
}


OptionsState::~OptionsState(void)
{
}

// IGameState Interface
/*virtual*/ void OptionsState::Enter ( void )
{
	font = CGame::GetInstance()->GetMenuFont();

	CursorPosition	= 0;
	keyPressedTimer = 0.0f;

	m_nBGVolume		= CGame::GetInstance()->GetBGVolume();
	m_nSFXVolume	= CGame::GetInstance()->GetSFXVolume();

	Fullscreen = !CGame::GetInstance()->GetIsWindowed();

	CurrentResolution = CGame::GetInstance()->GetResolution();
}

/*virtual*/ void OptionsState::Exit  ( void )	
{
	font = nullptr;
}

/*virtual*/ bool OptionsState::Input ( void )	
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if( pDI->KeyPressed(DIK_W) || pDI->KeyPressed(DIK_UP) )
	{
		CursorPosition--;	// 0 to Resolution

		if(CursorPosition < 0)
			CursorPosition = 4;

		//PlayMove();
	}
	else if( pDI->KeyPressed(DIK_S) || pDI->KeyPressed(DIK_DOWN) )
	{		
		CursorPosition++;	// 4 to Exit

		if( CursorPosition > 4 )
			CursorPosition = 0;

		//PlayMove();
	}

	if( pDI->KeyDown(DIK_A) || pDI->KeyDown(DIK_LEFT) )
	{
		if( keyPressedTimer >= 0.125f )
		{
			switch( CursorPosition )
			{
			case 0:
				{
					m_nBGVolume -= 0.01f;

					if( m_nBGVolume < 0.0f )
						m_nBGVolume = 0.0f;

					CGame::GetInstance()->SetBGVolume( m_nBGVolume );
				}
				break;

			case 1:
				{
					m_nSFXVolume -= 0.01f;

					if( m_nSFXVolume < 0.0f )
						m_nSFXVolume = 0.0f;

					CGame::GetInstance()->SetSFXVolume( m_nSFXVolume );

					//if( CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying( m_nSFXSoundID ) == false )
					//	CSGD_XAudio2::GetInstance()->SFXPlaySound( m_nSFXSoundID );
				}
				break;
			}

			keyPressedTimer = 0.0f;
		}
	}

	if( pDI->KeyDown(DIK_D) || pDI->KeyDown(DIK_RIGHT) )
	{
		if( keyPressedTimer >= 0.125f )
		{
			switch( CursorPosition )
			{
			case 0:
				{
					m_nBGVolume += 0.01f;

					if( m_nBGVolume < 0.0f )
						m_nBGVolume = 0.0f;

					CGame::GetInstance()->SetBGVolume( m_nBGVolume );
				}
				break;

			case 1:
				{
					m_nSFXVolume += 0.01f;

					if( m_nSFXVolume < 0.0f )
						m_nSFXVolume = 0.0f;

					CGame::GetInstance()->SetSFXVolume( m_nSFXVolume );

					//if( CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying( m_nSFXSoundID ) == false )
					//	CSGD_XAudio2::GetInstance()->SFXPlaySound( m_nSFXSoundID );
				}
				break;
			}

			keyPressedTimer = 0.0f;
		}
	}

	if( pDI->KeyPressed(DIK_RETURN) && CursorPosition == 4 )
	{
		//PlayEnter();
		CGame::GetInstance()->ChangeState( CGame::GetInstance()->GetPrevState() );	// Return to previous menu
	}

	return true;
}

/*virtual*/ void OptionsState::Update( float fElapsedTime )		
{
	keyPressedTimer += fElapsedTime;
}

/*virtual*/ void OptionsState::Render( void )			
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();

	float nBGVolume  = m_nBGVolume  * 100.0f;
	float nSFXVolume = m_nSFXVolume * 100.0f;

	TOSTRINGSTREAM BGVol;
	TOSTRINGSTREAM SFXVol;
	TOSTRINGSTREAM Resolution;
	TOSTRINGSTREAM isFullscreen;

	// Text for Volumes
	BGVol <<  (int)nBGVolume;
	SFXVol << (int)nSFXVolume;

	// Text for Resolutions
	//if(ResCount == 0)
	//	Resolution << Width::Small	<< "x" << Height::Short;
	//else if(ResCount == 1)
	//	Resolution << Width::Medium << "x" << Height::Tall;
	//else if(ResCount == 2)
	//	Resolution << Width::Large	<< "x" << Height::Grande;
	Resolution << CurrentResolution.width << "x" << CurrentResolution.height;


	// Text for Fullscreen
	if(Fullscreen == true)
		isFullscreen << "Fullscreen";
	else
		isFullscreen << "Windowed";

	// Draw it all to the screen
	if(CursorPosition == 0)
	{
		font->Draw(_T("BG Volume:"),			10,		400, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
		font->Draw(BGVol.str().c_str(),			500,	400, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
	}
	else
	{
		font->Draw(_T("BG Volume:"),			10,		400, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
		font->Draw(BGVol.str().c_str(),			500,	400, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
	}

	if(CursorPosition == 1)
	{
		font->Draw(_T("SFX Volume:"),			10,		475, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
		font->Draw(SFXVol.str().c_str(),		500,	475, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
	}
	else
	{
		font->Draw(_T("SFX Volume:"),			10,		475, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
		font->Draw(SFXVol.str().c_str(),		500,	475, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
	}

	if(CursorPosition == 2)
	{
		font->Draw(_T("Resolution:"),			10,		550, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
		font->Draw(Resolution.str().c_str(),	500,	550, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
	}
	else
	{
		font->Draw(_T("Resolution:"),			10,		550, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
		font->Draw(Resolution.str().c_str(),	500,	550, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
	}

	if(CursorPosition == 3)
	{
		font->Draw(_T("Fullscreen:"),			10,		625, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
		font->Draw(isFullscreen.str().c_str(),	500,	625, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
	}
	else
	{
		font->Draw(_T("Fullscreen:"),			10,		625, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
		font->Draw(isFullscreen.str().c_str(),	500,	625, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
	}

	if(CursorPosition == 4)
		font->Draw(_T("Back"),					10,		700, 1.0f, D3DCOLOR_XRGB(0, 255, 255));
	else
		font->Draw(_T("Back"),					10,		700, 1.0f, D3DCOLOR_XRGB(255, 255, 255));
}
